package com.example.smartcity_poverty.ui.AllService.TakeOutTr.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.Repository.toApi
import com.example.smartcity_poverty.logic.model.TakeOutModel.ToBannerModel
import com.example.smartcity_poverty.logic.model.TakeOutModel.ToServiceModel
import com.example.smartcity_poverty.logic.model.TakeOutModel.ToShopModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.TakeOutTr.adapter.ToServiceAdapter
import com.example.smartcity_poverty.ui.AllService.TakeOutTr.adapter.ToShopAdapter
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_take_home.*
import kotlinx.coroutines.Dispatchers


/**
 * 外卖订餐 - 主页
 */
class TakeHomeFragment : Fragment() {

    private val mTAG = this.javaClass.simpleName.toString()

    // 存储轮播图数据
    private var mBannerArray = ArrayList<ToBannerModel.Row>()

    // 存储外卖主题分类数据
    private var mServiceArray = ArrayList<ToServiceModel.Data>()

    // 存储店家列表数据
    private var mShopArray = ArrayList<ToShopModel.Row>()

    // 存储推荐商家列表数据
    private var mHotShopsArray = ArrayList<ToShopModel.Row>()


    // 外卖分类主题适配器
    private var mSerAdapter: ToServiceAdapter? = null

    // 店家列表适配器
    private var mShopAdapter: ToShopAdapter? = null

    // 推荐商家列表适配器
    private lateinit var mHotShopsAdapter: ToShopAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        onGetLocation()
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_take_home, container, false)
    }

    private fun onGetLocation() {

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        takeTitleFinish.setOnClickListener { activity?.finish() }
        // 轮播图
        onTakeHomeBannerImp(takeHomeBanner)
        // 外卖主题分类
        onTakeServiceImp()
        // 商家推荐
        getHotShopsList()
        // 店家列表
        onTakeShopList()
    }

    /**
     * 轮播图
     */
    private fun onTakeHomeBannerImp(takeHomeBanner: Banner<*, *>?) {
        coroutine(Dispatchers.Main) {
            // 发起网络请求获取轮播图数据
            val resultBanner = toApi.getToHomeBanner()
            // 判断是否获取成功
            if (resultBanner.code == 200) {
                // 先清空数据
                mBannerArray.clear()
                // 添加数据
                mBannerArray.addAll(resultBanner.rows)
            } else activity?.runOnUiThread {
                resultBanner.msg.show()
            }

            // 配置轮播图适配器
            takeHomeBanner?.adapter = object : BannerImageAdapter<ToBannerModel.Row>(mBannerArray) {
                override fun onBindView(
                    p0: BannerImageHolder?,
                    p1: ToBannerModel.Row?,
                    p2: Int,
                    p3: Int
                ) {
                    // 使用Glide加载图片
                    glide(url + p1?.advImg, p0?.imageView!!)
                }
            }

            takeHomeBanner?.addBannerLifecycleObserver(this@TakeHomeFragment)?.indicator =
                CircleIndicator(context)
            // 设置轮播图圆角
            takeHomeBanner?.setBannerRound(25f)
        }
    }




    /**
     * 外卖主题分类
     */
    private fun onTakeServiceImp() {
        // 开启协程发起网络请求
        coroutine(Dispatchers.Main) {
            val resultService = toApi.getToHomeService()
            // 判断是否获取成功
            if (resultService.code != 200) activity?.runOnUiThread {
                resultService.msg.show()
            }
            // 先将原先数据清空
            mServiceArray.clear()
            // 将数据添加到数组中
            mServiceArray.addAll(resultService.data)

            setSerAdapter()
        }
    }

    /**
     * 配置外卖列表列表适配器
     */
    private fun setSerAdapter() {
        if (mSerAdapter == null) {
            mSerAdapter = ToServiceAdapter(mServiceArray, R.layout.re_item_more)
        }

        takeRvService.apply {
            this.isNestedScrollingEnabled = false
            this.layoutManager = GridLayoutManager(context, 5)
            this.adapter = mSerAdapter
        }
        mSerAdapter?.notifyDataSetChanged()
    }



    /**
     * 店家列表
     */
    private fun onTakeShopList() = coroutine(Dispatchers.Main) {
        val resultShop = toApi.getShopList()
        // 判断是否获取成功
        if (resultShop.code != 200) activity?.runOnUiThread {
            resultShop.msg.show()
        }

        // 先将原先的数据清空
        mShopArray.clear()
        // 将数据添加到数组里面
        mShopArray.addAll(resultShop.rows)

        // 适配器
        setShopListAdapter()
    }

    private fun setShopListAdapter() {
        if (mShopAdapter == null) {
            mShopAdapter = ToShopAdapter(mShopArray, R.layout.re_take_shop)
        }

        takeRvShop.apply {
            this.isNestedScrollingEnabled = false
            this.layoutManager = LinearLayoutManager(context)
            this.adapter = mShopAdapter
        }
        mShopAdapter?.notifyDataSetChanged()
    }


    /**
     * 推荐商家
     * 获取推荐商家列表数据
     */
    private fun getHotShopsList() {
        // 开启协程
        coroutine(Dispatchers.Main) {
            val responseShopList = toApi.getShopList("Y")
            // is code response ok
            if (responseShopList.code != 200) {
                responseShopList.code.toString().show()
                return@coroutine
            }

            // clear array data
            mHotShopsArray.clear()
            // add array data
            mHotShopsArray.addAll(responseShopList.rows)

            // RecyclerView Adapter
            mHotShopsAdapter = ToShopAdapter(mHotShopsArray, R.layout.re_take_hot_shops)

            takeRvHotShops.apply {
                this.isNestedScrollingEnabled = false
                this.layoutManager = GridLayoutManager(context, 2).apply {
                    this.orientation = GridLayoutManager.HORIZONTAL
                }
                this.adapter = mHotShopsAdapter
            }
            mHotShopsAdapter.notifyDataSetChanged()
        }
    }

}